<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_LikeController extends Zend_Rest_Controller
{

	public function init()
	{
		$this->_helper->viewRenderer->setNoRender(true);
	}


	public function deleteAction() {
		try{
			$like_mapper = new Api_Model_LikeMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);
			$user_id = $this->_getParam('param1');

			if ($data['location_id'] != "" || $data['photo_id'] != ""){

				$like_mapper->unLike($data, $user_id);
				throw new Exception(json_encode(array('meta' => 'OK')),200);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function getAction() {

	}

	public function headAction() {

	}

	public function indexAction() {

	}

	public function postAction() {
		try{
			$like_mapper = new Api_Model_LikeMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);

			if ($data['location_id'] != "" || $data['photo_id'] != "" || $data['user_id'] != ""){

				$like_mapper->like($data);
				throw new Exception(json_encode(array('meta' => 'Created')),201);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function putAction() {
		try{
			$like_mapper = new Api_Model_LikeMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);
			$user_id = $this->_getParam('param1');

			if ($data['location_id'] != "" || $data['photo_id'] != ""){

				$like_mapper->unLike($data, $user_id);
				throw new Exception(json_encode(array('meta' => 'OK')),200);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}
}

